﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using MyOA.Model;

namespace MyOA.DAL
{
    public class DepartScheduleService
    {
        DBHelper db = new DBHelper();

        /// <summary>
        /// 获取全部的机构信息
        /// </summary>
        /// <returns>获取全部的机构信息</returns>
        public IList<BranchInfo> getAllBranchInfo()
        {
            IList<BranchInfo> branches = new List<BranchInfo>();
            using (DataTable dt = db.getDataTable("sp_getAllBranchInfo"))
            {
                foreach (DataRow row in dt.Rows)
                {
                    BranchInfo branch = new BranchInfo();
                    branch.BranchId = (int)row[0];
                    branch.BranchName = (string)row[1];
                    branch.BranchShortName = (string)row[2];

                    branches.Add(branch);
                }
            }

            return branches;
        }

        /// <summary>
        /// 根据机构ID获取相应的部门信息
        /// </summary>
        /// <param name="branchId">机构ID</param>
        /// <returns>部门信息集合</returns>
        public IList<DepartInfo> getDepartInfoByBranchId(int branchId)
        {

            IList<DepartInfo> departs = new List<DepartInfo>();
            using (DataTable dt = db.getDataTable("sp_getDepartInfoByBranchId",new SqlParameter("@branchId",branchId)))
            {
                foreach (DataRow row in dt.Rows)
                {
                    DepartInfo depart = new DepartInfo();
                    depart.DepartId = (int)row[0];
                    depart.DepartName = (string)row[1];
                  



                    departs.Add(depart);
                }
            }
            return departs;
        }


        /// <summary>
        /// 通过关键字搜索登陆名
        /// </summary>
        /// <param name="keyWord">关键字</param>
        /// <returns>获得的登录名集合</returns>
        public IList<string> getLoginIdByKeyWord(string keyWord)
        {
            IList<string> loginIds = new List<string>();
            using (SqlDataReader reader=db.getDataReader("sp_getUserLoginIdByKeyWord",new SqlParameter("@keyWord",keyWord)))
            {
                while (reader.Read())
                {
                    loginIds.Add((string)reader[0]);
                }
            }
            db.CloseConnection();
            return loginIds;
        }

        /// <summary>
        /// 根据多个不同条件来查找日程
        /// 注：部门ID为必须条件
        /// 如果不根据用户ID查找则设置USERID=0
        /// 如果不根据时间查找则设置DT=1900-1-1 0：00：00
        /// </summary>
        /// <param name="departId">条件:部门ID</param>
        /// <param name="userId">条件：用户ID</param>
        /// <param name="dt">条件：时间</param>
        /// <returns></returns>
        public IList<Schedule> getScheduleByMoreTerm(int departId, int userId, DateTime dt)
        {
            IList<Schedule> schedules = new List<Schedule>();
            string time = dt.ToShortDateString();
            SqlParameter[] values = new SqlParameter[]
            {
                new SqlParameter("@departId",departId),
                new SqlParameter("@userId",userId),
                new SqlParameter("@createTime",time)
            };
            using (SqlDataReader reader = db.getDataReader("sp_getScheduleByMoreTerm",values))
            {
                while (reader.Read())
                {
                    Schedule schedule = new Schedule();
                    schedule.ScheduleId = (int)reader[0];
                    schedule.Title = (string)reader[1];
                    schedule.Address = (string)reader[2];
                    schedule.BeginTime = (DateTime)reader[3];
                    schedule.EndTime = (DateTime)reader[4];
                    schedule.CreateTime = (DateTime)reader[5];
                    schedules.Add(schedule);
                }
            }
            db.CloseConnection();
            return schedules;
        }

        /// <summary>
        /// 根据用户登陆名获取用户ID
        /// </summary>
        /// <param name="loginId">用户登陆名</param>
        /// <returns>用户ID</returns>
        public int getUserIdByLoginId(string loginId)
        {
           
                return (int)db.getScalar("sp_getUserIdByLoginId", new SqlParameter("@loginId", loginId));
         
         
            
        }


    }
}
